#include "simple_browserside_handle.h"
#include "include/cef_parser.h"
#include "include/cef_app.h"

#include <iostream>

#include "Bll/SystemManager/User.h"
#include "Bll/SystemManager/Role.h"
#include "Bll/SystemManager/Department.h"
#include "Bll/SystemManager/SerialNum.h"

using namespace std;
bool SimpleBrowserSideHandler::OnQuery(CefRefPtr<CefBrowser> browser,
	CefRefPtr<CefFrame> frame, int64 query_id,
	const CefString& request, bool persistent, CefRefPtr<Callback> callback)
{
    try{
        _thread->GetTaskRunner()->PostTask(CefRefPtr<SimpleBrowserSideHandler::CefTaskData>(
            new SimpleBrowserSideHandler::CefTaskData(browser,request,callback)));
        return true;
    }catch(exception& e){
        callback->Failure(404,e.what());
        return false;
    }
}
bool SimpleBrowserSideHandler::Query(CefRefPtr<CefBrowser> browser,
		const CefString& request,CefRefPtr<Callback> callback)
{
    CefRefPtr<CefValue> value = CefParseJSON(request, JSON_PARSER_RFC);
    string module = value->GetDictionary()->GetString("module");
    string action = value->GetDictionary()->GetString("action");
    auto data = value->GetDictionary()->GetDictionary("data");
    if (module == "user") {
        UserManager::process(action,data,browser,callback);
    }else if(module=="department"){
        DepartmentManager::process(action,data,browser,callback);
    }else if(module=="role"){
        RoleManager::process(action,data,browser,callback);
    }else if(module=="serialnum"){
        SerialNumManager::process(action,data,browser,callback);
    }
    return true;
}
void SimpleBrowserSideHandler::OnQueryCanceled(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, int64 query_id)
{
	//cancel our async query task...
}